﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace DrawTools.Utils
{
    public static class ImageHelper
    {
        public static BitmapFrame ToBitmapFrame(this ImageSource image)
        {
            image.Dispatcher.VerifyAccess();

            var px2Wpf = 96 / Dpi.DpiValue;
            var visual = new DrawingVisual();
            
            image.Dispatcher.BeginInvoke(() =>
            {
                var dc = visual.RenderOpen();
                dc.DrawImage(image, new Rect(new Size(image.Width * px2Wpf, image.Height * px2Wpf)));
            });
            var renderBitmap = new RenderTargetBitmap((int)image.Width, (int)image.Height, px2Wpf, px2Wpf, PixelFormats.Pbgra32);
            renderBitmap.Render(visual);
            return BitmapFrame.Create(renderBitmap);
        }

        public static void Save(String filepath, params BitmapFrame[] frames)
        {
            BitmapEncoder encoder = null;

            switch (Path.GetExtension(filepath))
            {
                case ".jpg":
                case ".jpeg":
                    encoder = new JpegBitmapEncoder();
                    break;

                case ".png":
                    encoder = new PngBitmapEncoder();
                    break;

                default:
                    encoder = new BmpBitmapEncoder();
                    break;
            }
            lock (encoder)
            {
                foreach (var frame in frames)
                {
                    encoder.Frames.Add(frame);
                }
                filepath = filepath.Replace('/', '\\');
                var dir = Path.GetDirectoryName(filepath);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                if (File.Exists(filepath)) File.Delete(filepath);
                using (var fs = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Read))
                {
                    lock (fs)
                    {
                        encoder.Save(fs);
                    }
                }
            }
        }

        /// <summary>
        /// </summary>
        /// <param name="extension">文件后缀：.jpg;.jpeg;.png</param>
        /// <param name="frames"></param>
        /// <returns></returns>
        public static BitmapSource GetBitmapSource(string extension, params BitmapFrame[] frames)
        {
            BitmapEncoder encoder = null;
            switch (extension)
            {
                case ".jpg":
                case ".jpeg":
                    encoder = new JpegBitmapEncoder();
                    break;

                case ".png":
                    encoder = new PngBitmapEncoder();
                    break;

                default:
                    encoder = new BmpBitmapEncoder();
                    break;
            }
            lock (encoder)
            {
                foreach (var frame in frames)
                {
                    encoder.Frames.Add(frame);
                }
                return encoder.Preview.Clone();
            }
        }
    }
}